﻿using System;
using Helpers;
using QDT.Common;

namespace QDT.MysqlToSqlServer.ImportTables
{
    public class ImportProject : ImportTables
    {
        public void Import()
        {

            var sqlHelper = new SqlHelper(SqlServer);
            var mysql = string.Format("select * from qdt_deal");
            var dt = sqlHelper.ExecuteDataTable(mysql);
            var sql = "";
            var n = 0;
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                sql += @"INSERT INTO qdt_project (Id,ParentId,Name,SubName,UserId,TypeId,IsEffect,IsDelete,Sort,RepaymentID,BorrowAmount,MinLoanMoney,MaxLoanMoney,RepayTime,
                            RepayTimeType,Rate,Status,LoadMoney,RepayMoney,CreateTime,StartTime,RepayStartTime,LastRepayTime,NextRepayTime,RepayEndTime,BadTime,
                            CurrentStage,TotalStage,AgreementLink,IsOffline,SettleDay) 
                       VALUES ('{0}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}',
                                '{23}','{24}','{25}','{26}','{27}','{28}','{29}');"
                       .FormatWith(dt.Rows[i]["id"], dt.Rows[i]["name"], dt.Rows[i]["sub_name"], dt.Rows[i]["user_id"], dt.Rows[i]["cate_id"],
                       dt.Rows[i]["is_effect"], dt.Rows[i]["is_delete"], dt.Rows[i]["sort"], dt.Rows[i]["loantype"], dt.Rows[i]["borrow_amount"],
                       dt.Rows[i]["min_loan_money"], dt.Rows[i]["max_loan_money"], dt.Rows[i]["repay_time"], dt.Rows[i]["repay_time_type"], dt.Rows[i]["rate"],
                       dt.Rows[i]["deal_status"], dt.Rows[i]["load_money"], dt.Rows[i]["repay_money"], dt.Rows[i]["create_time"], dt.Rows[i]["start_time"],
                       dt.Rows[i]["repay_start_time"], dt.Rows[i]["last_repay_time"], dt.Rows[i]["next_repay_time"], dt.Rows[i]["repay_end_time"], dt.Rows[i]["bad_time"],
                       dt.Rows[i]["CurrentStage"], dt.Rows[i]["TotalStage"], dt.Rows[i]["AgreementLink"], dt.Rows[i]["IsOffline"], dt.Rows[i]["SettleDay"]);

                sql += @"INSERT INTO qdt_project_parent (Id,Name,SubName,TypeId,AgencyId,UserId,RepaymentId,Description,IsEffect,IsDelete,Sort,BorrowAmount,RepayTime,RepayTimeType,
                            Rate,EndDate,CreateTime,SettleDay,Warrant,LoanPurpose,TotalBorrow,Industry,IndustryDetail,LoanInfo,ProjectProcess,RiskControl) 
                       VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}',
                                '{23}','{24}','{25}');"
                       .FormatWith(dt.Rows[i]["id"], dt.Rows[i]["name"], dt.Rows[i]["sub_name"], dt.Rows[i]["cate_id"], dt.Rows[i]["AgencyId"], dt.Rows[i]["user_id"],
                       dt.Rows[i]["loantype"], dt.Rows[i]["description"], dt.Rows[i]["is_effect"], dt.Rows[i]["is_delete"], dt.Rows[i]["sort"], dt.Rows[i]["borrow_amount"],
                       dt.Rows[i]["repay_time"], dt.Rows[i]["repay_time_type"], dt.Rows[i]["rate"], dt.Rows[i]["enddate"], dt.Rows[i]["create_time"], dt.Rows[i]["SettleDay"],
                       dt.Rows[i]["Warrant"], dt.Rows[i]["LoanPurpose"],
                       dt.Rows[i]["TotalBorrow"], "","","","","");

                if ((i + 1) % 30 != 0) continue;
                n += sqlHelper.ExecuteNonQuery(sql);
                sql = "";
            }
            if (!string.IsNullOrWhiteSpace(sql))
                n += sqlHelper.ExecuteNonQuery(sql);
            Console.WriteLine("成功导入Project表：" + n + "条数据！");
        }
    }
}
